<?php

/**
 * @name Functions
 * @author Jeroen van der Laan & Pascal de Vink
 * @copyright UNET BV 2007
 * @version 1.0
 */

class Xml {
	
	private $xmldocument;
	private $rootelement;
	
	/**
	 * Constructor
	 * 
	 * @param string $rootelement
	 */
	function Xml($rootelement='', $standard = true) {
		$this->xmldocument = new DOMDocument('1.0');
		
		if ($rootelement != '') {
			$this->setRootElement($rootelement);
		} else {
			$this->setRootElement('sso');
		}
		
		if ($standard == true) {
			$this->createElement('message_header');
			$this->createElement('message_body');
		}
	}

	/**
	 * Sets the roottag of the XML file. 
	 * 
	 * @param string $element
	 */
	function setRootElement($element) {
		$this->rootelement = $element;
		$root = $this->xmldocument->createElement($element);
		$root = $this->xmldocument->appendChild($root);
	}
	
	/**
	 * Set a new element into the xmlDocument. 
	 * ElementValue and parent are optional components
	 * 
	 * @param string $elementname
	 * @param string $elementvalue
	 * @param string $parent
	 * @param int id 
	 */
	function createElement($elementname, $elementvalue='', $parent='', $id='') {
		//set element
		$element = $this->xmldocument->createElement($elementname);
		
		//check if parent was given, else root
		if ($parent == '') {
			$element = $this->getElementsByTagName($this->rootelement)->item(0)->appendChild($element);	
		} else {
			$element = $this->getElementsByTagName($parent)->item($id)->appendChild($element);
		}
		
		if ($elementvalue != '') {
			$value = $this->xmldocument->createTextNode($elementvalue);
			$value = $element->appendChild($value);
		} 
	}
	
	/**
	 * Create a attribute within an element
	 * 
	 * @param string $name
	 * @param string $element
	 * @param string $value
	 * @param int itemnr
	 */
	function createAttribute($name, $value, $element, $itemnr) {
		$attr = $this->xmldocument->createAttribute($name);
		$attr = $this->xmldocument->getElementsByTagName($element)->item($itemnr)->appendChild($attr);
		$this->xmldocument->getElementsByTagName($element)->item($itemnr)->setAttribute($name, $value);
	}
	
	/**
	 * Create CDATA into an element
	 * 
	 * @param string $data
	 * @param string $element
	 */
	function createCDATA($data, $element) {
		$cdata = $this->xmldocument->createCDATASection($data);
		$cdata = $element->appendChild($cdata);
	}
	
	/**
	 * Get an array of tags containing the given Tag Name
	 * 
	 * @param string $name
	 * @return element
	 */
	function getElementsByTagName($name) {
		return $this->xmldocument->getElementsByTagName($name);
	}
	
	/**
	 * Get the Attribute from a given element.
	 * 
	 * @param string $element
	 * @param string $attr
	 * @param string $item
	 * @return attribute
	 */ 
	function getAttribute($element, $attr, $item='') {
		if ($item == '') {
			return $this->xmldocument->getElementsByTagName($element)->item(0)->getAttribute($attr);
		} else {
			return $this->xmldocument->getElementsByTagName($element)->item($item)->getAttribute($attr);
		}
	}
	
	/**
	 * Get the value of a tag given as an argument. 
	 * When multiple tags of the same name are used
	 * also a number is needed.
	 * 
	 * @param string $element
	 * @param string $item
	 * @return content
	 */
	function getContentOfChild($element, $item='') {
		if ($item == '') {
			return $this->xmldocument->getElementByTagName($element)->item(0)->textContent;
		} else {
			return $this->xmldocument->getElementByTagName($element)->item($item)->textContent;
		}
	}
	
	/**
	 * Load an existing XML document into this structure
	 * 
	 * @param string $location
	 */
	function load($location){
		$this->xmldocument->load($location);
	}
	
	/**
	 * Save a XML structure into a file
	 * 
	 * @param string $filename
	 */
	function save($filename) {
		$this->xmldocument->save($filename);
	}
	
	/**
	 * Get the complete structure
	 * 
	 * @return boolean
	 */
	function getComplete() {
		return $this->xmldocument->saveXML();
	}
	
	/**
	 * Get the DomElement from the parent
	 * 
	 * @param string $element
	 * @param int $number
	 * @return element
	 */
	function getDomElement($element, $number) {
		return $this->xmldocument->getElementsByTagName($element)->item($number);
	}
	
	/**
	 * Get the main DomDocument object from the parent
	 * 
	 * @return xmldocument
	 */
	function getDomDocument(){
		return $this->xmldocument;
	}
	
	
	
	
	
	
	
	/**
	 * Creates the standard UNET header tags
	 * 
	 * Only on standard XML
	 */
	function setHeaderTags($service, $message_type, $message_version, $orginator, $user, $password) {
		$this->setService($service);
		$this->createElement('message_type', $message_type, 'message_header', 0);
		$this->createElement('message_version', $message_version, 'message_header', 0);
		$this->createElement('orginator', $orginator, 'message_header', 0);
		$this->createElement('timestamp', time(), 'message_header', 0);
		$this->setXMLUser($user);
		$this->setXMLPassword($password);
	}
	
	
	
	/**
	 * Creates the standard UNET service tag
	 * 
	 * Only on standard XML
	 */
	function setService($service) {
		$this->createElement('service', $service, 'message_header', 0);
	}
	
	/**
	 * Creates the standard UNET xmluser tag
	 * 
	 * Only on standard XML
	 */
	function setXMLUser($user) {
		$this->createElement('username', $user, 'message_header', 0);
	}
	
	
	/**
	 * Creates the standard UNET xmlpassword tag
	 * 
	 * Only on standard XML
	 */
	function setXMLPassword($password) {
		$this->createElement('password', $password, 'message_header', 0);
	}
	
	
	
	
	
	
}

?>